<!--  -->
<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10"
          ><div class="text item">
            <el-input placeholder="请输入内容" class="input-with-select"
            v-model="queryInfo.query" clearable @clear="getUserList">
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="4">
          <!-- <el-button type="primary" @click="addUser">添加用户</el-button> -->
        </el-col>
      </el-row>
      <!-- 用户列表区 -->
      <el-table :data="userList" highlight-current-row  border>
        <el-table-column
          type="index"
          width="100">
        </el-table-column>
        <el-table-column
          property="header"
          label="头像"
          width="140">
          <template slot-scope="scope">
      　　　　<img :src="scope.row.header" class="headimag"/>
      　　</template>
        </el-table-column>
        <el-table-column
          property="username"
          label="用户名"
          width="210">
        </el-table-column>
        <el-table-column
          property="mobile"
          label="手机号"
          width="190">
        </el-table-column>
        <el-table-column
          property="createTime"
          label="注册时间"
          width="220">
          <template slot-scope="scope">
            {{scope.row.createTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <!-- <el-button type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row)">修改</el-button> -->
            <el-button type="danger" icon="el-icon-delete" @click="removeUserInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
        
      </el-table>
      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[2, 3,5]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>

  <!-- 添加用户框展示 -->
  <el-dialog title="添加用户信息" width="50%" :visible.sync ="dialogFormVisible"
    @close="addDialogClose">
      <el-form
        :model="addUserInfo"
        :rules="addRules"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm addressUpdate"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input
            type="password"
            v-model="addUserInfo.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="addUserInfo.mobile"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="header">
          <img :src="addUserInfo.header" alt="" class="headimag">
          <OssUpload v-model="addUserInfo.header"></OssUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUserInfo">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户对话框 -->
    <!-- <el-dialog title="修改用户信息" width="50%" :visible.sync ="dialogEditVisible"
    @close="editDialogClose">
      <el-form
        :model="updateUserInfo"
        :rules="addRules"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm addressUpdate"
      >
        <el-form-item label="头像" prop="header">
          <img :src="updateUserInfo.header" alt="" class="headimag">
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="updateUserInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="updateUserInfo.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataInfo">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import OssUpload from '../../components/OssUpload.vue'
export default {
  name: "Userlist",
  data() {
    return {
      userList: [
      ],
      queryInfo:{
        query:'',
        // 当前页码
        pageNum:1,
        // 当前每页显示多少条数据
        pageSize:2
      },
      updateUserInfo:{
        header:'',
        username:'',
        mobile:''
      },
      total:10,
      // 添加用户的表单数据
      addUserInfo:{
        username: "",
        password: "",
        mobile: "",
        header: "",
      },
      // 添加用户框的显示与隐藏
      dialogFormVisible:false,
      // 修改对话框的显示与隐藏
      dialogEditVisible:false,
      addRules:{
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 3, max: 15, message: "只能输入3-15个字母、数字、下划线" },
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 15, message: "只能输入6-15个字母、数字、下划线" },],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: "请输入合法手机号/电话号",
            trigger: "blur",
          },
        ],
      }
    };
  },
  components:{
    OssUpload
  },
  created(){
    this.getUserList()
  },
  methods:{
    // 监听pageSize改变
    handleSizeChange(newSize){
      // console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getUserList()
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage){
      // console.log(newPage)
      this.queryInfo.pageNum = newPage
       this.getUserList()
    },
    // 获取用户列表
     async getUserList(){
        const{data:res} = await this.$http.get(`dqxmall/userinfo/list?page=${this.queryInfo.pageNum}&limit=${this.queryInfo.pageSize}&key=${this.queryInfo.query}`)
        console.log(res)
        if(res.code === 0){
          this.total = res.page.totalCount
          this.userList = res.page.list
        }
    },
    // 显示用户框
    addUser(){
      this.dialogFormVisible=true

    },
    // 显示修改对话框
    showEditDialog(row){
      this.dialogEditVisible = true,
      // console.log(row)
      this.updateUserInfo = row
    },
    // 关闭对话框
    addDialogClose(){
      this.$refs.addFormRef.resetFields()
    },
    // 关闭修改对话框
    editDialogClose(){
      this.$refs.editFormRef.resetFields()
    },
    // 添加用户
    submitUserInfo(){
      this.dialogFormVisible=false
    },
    
    // 点击删除用户
    async removeUserInfo(id){
      // 弹框询问是否确认删除
        const confirmResult = await this.$confirm('确定删除该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch((error) => {error    });
        // 如果确认删除，则返回字符串confirm
        // 如果取消删除，则返回字符串cancel
        if(confirmResult !=='confirm'){
          this.$message.info("已取消删除")
        }else{
          // 发送删除请求
           const {data:res} = await this.$http.post("/dqxmall/userinfo/delete",[id])
            if(res.code ===  0){
              this.getUserList()
              this.$message.success("用户信息删除成功")
            }
            else{
              this.$message.error("用户信息删除失败")
            }
        }
    }
  }
};
</script>
<style lang="less" scoped>

.headimag{
  height: 70px;
  width: 70px;
}
</style>
